package com.example.hotelmanagement.controller;

import com.example.hotelmanagement.entity.User;
import com.example.hotelmanagement.service.UserService;
import com.example.hotelmanagement.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 注册用户
    @PostMapping("/register")
    public ResponseResult<Void> registerUser(@RequestBody User user) {
        boolean success = userService.registerUser(user);
        return success ? ResponseResult.success() : ResponseResult.failure("Registration failed");
    }

    // 用户登录
    @PostMapping("/login")
    public ResponseResult<User> login(@RequestParam String username, @RequestParam String password) {
        User user = userService.authenticate(username, password);
        return user != null ? ResponseResult.success(user) : ResponseResult.failure("Login failed");
    }

    // 根据ID获取用户
    @GetMapping("/{id}")
    public ResponseResult<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return user != null ? ResponseResult.success(user) : ResponseResult.failure("User not found");
    }

    // 更新用户信息
    @PutMapping("/update")
    public ResponseResult<Void> updateUser(@RequestBody User user) {
        boolean success = userService.updateUser(user);
        return success ? ResponseResult.success() : ResponseResult.failure("Update failed");
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Void> deleteUser(@PathVariable Long id) {
        boolean success = userService.deleteUser(id);
        return success ? ResponseResult.success() : ResponseResult.failure("Delete failed");
    }
}
